<template>
  <div class="router-tutorial">
    <div class="tutorial-content">
      <h2>动态路由匹配</h2>
      <p>很多时候，我们需要将给定匹配模式的路由映射到同一个组件。例如，我们可能有一个 User 组件，它应该对所有用户进行渲染，但用户 ID 不同。</p>
      
      <h3>路径参数</h3>
      <p>在 Vue Router 中，我们可以使用动态路径参数来捕获这种情况：</p>
      <pre>
const routes = [
  { path: '/user/:id', component: User }
]</pre>
      
      <p>现在像 <code>/user/1</code> 和 <code>/user/2</code> 的 URL 都会映射到同一个路由。</p>
      
      <h3>获取路由参数</h3>
      <p>在组件中，你可以通过 <code>$route.params</code> 或者 <code>useRoute()</code> 获取动态参数：</p>
      <pre>
// 在 &lt;script setup&gt; 中
import { useRoute } from 'vue-router'
const route = useRoute()
// 获取路由参数
const userId = route.params.id</pre>
      
      <h3>响应路由参数变化</h3>
      <p>使用带有参数的路由时，要注意当用户从 <code>/user/1</code> 导航到 <code>/user/2</code> 时，相同的组件实例将被重用。这意味着组件的生命周期钩子不会被调用。你可以使用 watch 监听路由参数变化：</p>
      <pre>
import { watch } from 'vue'
import { useRoute } from 'vue-router'

const route = useRoute()
watch(
  () => route.params,
  (newParams) => {
    // 做出响应...
  }
)</pre>
    </div>
    
    <div class="code-editor">
      <div class="file-tabs">
        <button @click="currentFile = 'App'">App.vue</button>
        <button @click="currentFile = 'User'">User.vue</button>
        <button @click="currentFile = 'router'">router.js</button>
      </div>
      
      <div class="code-preview" v-if="currentFile === 'App'">
        <pre><code>{{ appTemplate }}</code></pre>
      </div>
      
      <div class="code-preview" v-if="currentFile === 'User'">
        <pre><code>{{ userTemplate }}</code></pre>
      </div>
      
      <div class="code-preview" v-if="currentFile === 'router'">
        <pre><code>{{ routerTemplate }}</code></pre>
      </div>
      
      <div class="result-preview">
        <h4>预览效果：</h4>
        <div class="preview-container">
          <div class="app-container">
            <h1>动态路由示例</h1>
            <div class="nav-links">
              <a href="#" @click.prevent="setUserId(1)">用户1</a> | 
              <a href="#" @click.prevent="setUserId(2)">用户2</a> | 
              <a href="#" @click.prevent="setUserId(3)">用户3</a>
            </div>
            <div class="route-view">
              <User />
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import User from './components/User.vue';

const currentFile = ref('App');
const userId = ref('1');

function setUserId(id) {
  userId.value = id;
  // 在真实应用中，这里会使用router.push进行导航
}

// 预定义模板
const appTemplate = `<template>
  <div id="app">
    <h1>动态路由示例</h1>
    <div class="nav-links">
      <router-link to="/user/1">用户1</router-link> | 
      <router-link to="/user/2">用户2</router-link> | 
      <router-link to="/user/3">用户3</router-link>
    </div>
    <router-view></router-view>
  </div>
</template>

<script setup>
// App.vue 不需要特殊逻辑
<\/script>

<style>
.nav-links {
  margin: 20px 0;
}
</style>`;

const userTemplate = `<template>
  <div class="user">
    <h2>用户页面</h2>
    <p>用户 ID: {{ userId }}</p>
  </div>
</template>

<script setup>
import { useRoute } from 'vue-router';

const route = useRoute();
// 从路由参数中获取用户ID
const userId = route.params.id;
<\/script>

<style scoped>
.user {
  padding: 20px;
  background-color: #f7e8fd;
  border-radius: 5px;
}
</style>`;

const routerTemplate = `import { createRouter, createWebHistory } from 'vue-router'
import User from './components/User.vue'

const routes = [
  {
    path: '/user/:id',
    name: 'user',
    component: User
    // 路由参数 id 将作为 props 传递给组件
    // props: true
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

export default router`;
</script>

<style scoped>
.router-tutorial {
  display: flex;
  height: 100%;
}

.tutorial-content {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
  border-right: 1px solid #ddd;
}

.code-editor {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.file-tabs {
  display: flex;
  background-color: #f0f0f0;
  border-bottom: 1px solid #ddd;
}

.file-tabs button {
  padding: 8px 16px;
  border: none;
  background: none;
  cursor: pointer;
}

.file-tabs button:hover {
  background-color: #e0e0e0;
}

.code-preview {
  flex: 1;
  overflow-y: auto;
  padding: 10px;
  background-color: #f9f9f9;
}

pre {
  margin: 0;
  white-space: pre-wrap;
  font-family: monospace;
}

.result-preview {
  height: 300px;
  border-top: 1px solid #ddd;
  padding: 10px;
  overflow-y: auto;
}

.preview-container {
  border: 1px solid #ddd;
  border-radius: 5px;
  padding: 15px;
  background-color: white;
}

.app-container {
  text-align: center;
}

.nav-links {
  margin: 20px 0;
}

.route-view {
  margin-top: 20px;
  min-height: 100px;
}
</style> 